package org.lxh.demo;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MyWebViewDemo extends Activity {
	private WebView webview = null;

	@SuppressLint("SetJavaScriptEnabled") @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.webview = (WebView) super.findViewById(R.id.webview);
		this.webview.getSettings().setJavaScriptEnabled(true); // ����JavaScript
		this.webview.setWebChromeClient(new WebChromeClientImpl());
		webview.getSettings().setDomStorageEnabled(true);   
		webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		webview.getSettings().setAppCacheMaxSize(1024*1024*8);  
		String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();  
		webview.getSettings().setAppCachePath(appCachePath);  
		webview.getSettings().setAllowFileAccess(true);  
		webview.getSettings().setAppCacheEnabled(true); 
		this.webview.loadUrl("file:///android_asset/html/index.html");
	}
	public boolean onKeyDown(int keyCode,KeyEvent event){
		if((keyCode==KeyEvent.KEYCODE_BACK)&& webview.canGoBack()){
			webview.goBack();
			return true;
		}
//		Intent intent=new Intent();
//		intent.setClass(MyWebViewDemo.this,MyWebViewDemo.class);
//		    startActivity(intent);
	    finish();
		return false;
	}
	private class WebChromeClientImpl extends WebChromeClient {
        
	      
		@SuppressWarnings("unused")
		public boolean shouldOverrideUrlLoading(WebView view,String url){
			view.loadUrl(url);
			return true;
			}
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
         
                           Builder builder = new Builder(MyWebViewDemo.this);
                               builder.setTitle("遂平银行");
                               builder.setMessage(message);
                               builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                                   @Override
                                   public void onClick(DialogInterface dialog, int which) {
                                       result.confirm();
                                   }
                               });
                              builder.setCancelable(false);
                               builder.create();
                               builder.show();
                               return true;
                           }
	}
}